package socilgirl.dell.mydemo.view;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import socilgirl.dell.mydemo.R;

public class PendingIntentActivity extends AppCompatActivity {

    static final int NOTIFICATION_ID = 0x123;
    NotificationManager nm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_intent);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        findViewById(R.id.btn_sent_notice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });
        findViewById(R.id.btn_dele_notice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dele();
            }
        });
    }

    private void send() {
        Intent intent =new Intent(PendingIntentActivity.this,CardBindActivity.class);
        PendingIntent pi = PendingIntent
                .getActivity(PendingIntentActivity.this, 0, intent, 0);
        Notification notification = new Notification
                .Builder(this)
                .setAutoCancel(true)
                .setTicker("有新消息")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("有一条新消息")
                .setContentText("恭喜您，您中奖了，奖金500万！")
                .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pi).build();
        nm.notify(NOTIFICATION_ID, notification);
    }

    private void dele() {
//        nm.cancel(NOTIFICATION_ID);
        nm.cancelAll();
    }
}
